package com.blast.manager.net.presenter

import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.manager.R
import com.blast.manager.adapter.ReadyAuthErrorAdapter
import com.blast.manager.bean.ReadyErrorModel
import com.blast.manager.delay.ui.UnauthorizedActivity
import com.blast.manager.net.ui.NotMatchActivity
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.util.ObservableUtil
import io.reactivex.ObservableOnSubscribe
import kotlinx.android.synthetic.main.layout_net_error.view.*

class NetCheckErrorPresenter : AbsNetCheckPresenter(){

     var isBlast = false

    override fun showView(vararg args: Any) {

        if(args[0] is Boolean) {
            isBlast = args[0] as Boolean
        }

        rootView.netCheckErrorListView.layoutManager = LinearLayoutManager(context)

        rootView.netCheckErrorBackBtn.setOnClickListener {
            if(rootView.netCheckErrorBackBtn.isShown) {
                if (isBlast) {
                    getBlastActivity().finish()
                } else {
                    getActivity().finish()
                }
            }
        }


        if(args.isNotEmpty() && args.size >1 ) {
            if(args[1] is Boolean) {
                isBlast = args[1] as Boolean
            }
            val noAuthData = args[0] as ArrayList<LeiGuanEntity>
            val noAuthError = arrayListOf<ReadyErrorModel>()
            val model = ReadyErrorModel()
            model.title = "雷管未授权"
            model.widget = "发"
            model.des = noAuthData.size.toString()
            noAuthError.add(model)

            val mReadyAuthErrorAdapter = ReadyAuthErrorAdapter(noAuthError)
            rootView.netCheckErrorListView.adapter = mReadyAuthErrorAdapter

            rootView.netCheckErrorNotMatchBtn.visibility = View.GONE
            rootView.netCheckErrorNotAuthBtn.visibility = View.VISIBLE

            rootView.netCheckErrorNotAuthBtn.setOnClickListener {
                if (rootView.netCheckErrorNotAuthBtn.isShown) {
                    UnauthorizedActivity.start(context,noAuthData as ArrayList)
                }
            }
        }


        if(!isBlast) {
            getActivity().dismissButtonLayout()
            getActivity().dismissClearAllPop()
        } else {
            getBlastActivity().dismissButtonLayout()
            getBlastActivity().dismissClearAllPop()
        }
    }


    /**
     * 检查雷管不匹配信息
     */
    fun checkNotMatchData(lgs: ArrayList<LeiGuanEntity>,isBlast:Boolean){
        var errorNum = 0
        val notMatchArray = arrayListOf<LeiGuanEntity>()
        ObservableUtil.doNext(ObservableOnSubscribe {
            val nma = arrayListOf<ReadyErrorModel>()
            lgs.forEach {
                if(TextUtils.equals("0",it.state.toCharArray()[5].toString())){
                    errorNum ++
                    notMatchArray.add(it)
                } else if(TextUtils.equals("0",it.state.toCharArray()[6].toString())){
                    errorNum++
                    notMatchArray.add(it)
                }
            }
            if(errorNum > 0){
                val rem = ReadyErrorModel()
                rem.des = errorNum.toString()
                rem.title = "雷管不匹配："
                rem.widget = "发"
                nma.add(rem)
            }
            it.onNext(nma)
        },object : ObservableUtil.Companion.OnNextCallback<List<ReadyErrorModel>>(){
            override fun onNext(t: List<ReadyErrorModel>) {
                if(t.isNotEmpty()){
                    show(isBlast)
                    val mReadyAuthErrorAdapter = ReadyAuthErrorAdapter(t)
                    rootView.netCheckErrorListView.adapter = mReadyAuthErrorAdapter
                    rootView.netCheckErrorNotMatchBtn.visibility = View.VISIBLE
                    rootView.netCheckErrorNotAuthBtn.visibility = View.GONE
                    rootView.netCheckErrorNotMatchBtn.setOnClickListener {
                        if(rootView.netCheckErrorNotMatchBtn.isShown){
                            NotMatchActivity.start(context,notMatchArray,false)
                        }
                    }
                } else {
                    if(isBlast){
                        getBlastActivity().dismissClearAllPop()
                        getBlastActivity().showChargeReadyLayout()
                    } else {
                        getActivity().showText(false,"验证成功。\n 检测到雷管：${lgs.size} 发")
                    }
                }
            }
        })

    }


    fun showBlastError(data:List<ReadyErrorModel>,notAuthArray:List<LeiGuanEntity>,notMatchArray:List<LeiGuanEntity>,notChargeArray :List<LeiGuanEntity>){
        show(true)
        val mReadyAuthErrorAdapter = ReadyAuthErrorAdapter(data)
        rootView.netCheckErrorListView.adapter = mReadyAuthErrorAdapter
        rootView.blastErrorButtonLayout.visibility = View.VISIBLE
        rootView.netErrorButtonLayout.visibility = View.GONE

        rootView.blastCheckErrorBackBtn.setOnClickListener {
            if(rootView.blastCheckErrorBackBtn.isShown) {
                getBlastActivity().finish()
            }
        }

        rootView.blastCheckErrorNotChargeBtn.visibility = if(notChargeArray.isNotEmpty()) View.VISIBLE else View.INVISIBLE
        rootView.blastCheckErrorNotChargeBtn.setOnClickListener {
            if(rootView.blastCheckErrorNotChargeBtn.isShown) {
                NotMatchActivity.start(context,notMatchArray as ArrayList,true)
            }
        }

        rootView.blastCheckErrorNotAuthBtn.visibility = if(notAuthArray.isNotEmpty()) View.VISIBLE else View.INVISIBLE
        rootView.blastCheckErrorNotAuthBtn.setOnClickListener {
            if(rootView.blastCheckErrorNotAuthBtn.isShown) {
                UnauthorizedActivity.start(context,notAuthArray as ArrayList)
            }
        }

        rootView.blastCheckErrorNotMatchBtn.visibility = if(notMatchArray.isNotEmpty()) View.VISIBLE else View.INVISIBLE
        rootView.blastCheckErrorNotMatchBtn.setOnClickListener {
            if(rootView.blastCheckErrorNotMatchBtn.isShown) {
                NotMatchActivity.start(context,notMatchArray as ArrayList,false)
            }
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.layout_net_error
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if(isAdd()){
            if(keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK){
                rootView.netCheckErrorBackBtn.performClick()
                rootView.blastCheckErrorBackBtn.performClick()
            }
            if(keyCode == KeyEvent.KEYCODE_F2){
                rootView.blastCheckErrorNotChargeBtn.performClick()
            }
            if(keyCode == KeyEvent.KEYCODE_F3){
                rootView.blastCheckErrorNotAuthBtn.performClick()
            }
            if(keyCode == KeyEvent.KEYCODE_F4){
                rootView.netCheckErrorNotMatchBtn.performClick()
                rootView.netCheckErrorNotAuthBtn.performClick()
                rootView.blastCheckErrorNotMatchBtn.performClick()
            }
        }
        return false
    }
}